package testbim;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Client extends Application {
    DataOutputStream toServer = null;
    DataInputStream fromServer = null;

    @Override
    public void start(Stage primaryStage) throws Exception {
        GridPane grid = new GridPane();
//        grid.setGridLinesVisible(true);
        grid.setAlignment(Pos.CENTER);
        //行列之间的间隔
        grid.setHgap(10);
        grid.setVgap(10);
        //面板边缘周围的间隔
        grid.setPadding(new Insets(0, 25, 25, 25));

        Text scenetitle = new Text("请输入身高体重，为你检查你的BMI指标：");
		scenetitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 20));
        grid.add(scenetitle, 0, 0, 2, 1);

        Label height = new Label("身高:");
        grid.add(height, 0, 2);
        TextField heightField = new TextField();
        heightField.setPromptText("身高（m）");
        grid.add(heightField, 1, 2);

        Label weight = new Label("体重:");
        grid.add(weight, 0, 3);
        TextField weightField = new TextField();
        weightField.setPromptText("体重（kg）");
        grid.add(weightField, 1, 3);

        Button btn = new Button("提交");
        HBox hbBtn = new HBox(10);
        hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
        hbBtn.getChildren().add(btn);
        grid.add(hbBtn, 1, 4);


        Label text = new Label("检测结果：");
        grid.add(text, 0, 4);
        TextArea textArea = new TextArea();
        grid.add(textArea,1,5);


        Scene scene = new Scene(grid,600,400);
        primaryStage.setTitle("Client");
        primaryStage.setScene(scene);
        primaryStage.show();

        btn.setOnAction(e -> {
            try{
                double h = Double.parseDouble(heightField.getText().trim());
                double w = Double.parseDouble(weightField.getText().trim());

                toServer.writeDouble(h);
                toServer.writeDouble(w);
                toServer.flush();

                double bmi = fromServer.readDouble();
                String result = fromServer.readUTF();

                textArea.appendText("身高："+h+"   体重："+w+"\n");
                textArea.appendText("BMI为: "+bmi+"  属于："+result+"\n");


            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });

        try {
            Socket socket = new Socket("localhost",8000);

            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        launch(args);
    }
}

